package com.cheejee.goodscenter.controller;

import static com.cheejee.goodscenter.module.response.BaseReponse.addOk;
import static com.cheejee.goodscenter.module.response.BaseReponse.deleteOk;
import static com.cheejee.goodscenter.module.response.BaseReponse.queryOk;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cheejee.goodscenter.module.entity.Category;
import com.cheejee.goodscenter.module.vo.CategoryVo;
import com.cheejee.goodscenter.service.CategoryService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author CARRY
 * @Date 2020年10月30日下午3:03:28
 */
@RestController
@RequestMapping("/category")
@Api(tags = "分类")
public class CategoryController {

	@Resource
	private CategoryService service;
	
	@GetMapping("/L1")
	@ApiOperation("查询所有一级分类")
	public Object getL1Category() {
		
		return queryOk(service.queryCategory(null, null, (byte)1, 0, 0));
	}
	
	@GetMapping("/{id}")
	@ApiOperation("按ID查询分类")
	public Object getCategory(@PathVariable Integer id) {
		
		return queryOk(service.queryCategoryById(id));
	}
	
	@GetMapping("/{id}/son")
	@ApiOperation("查询子分类")
	public Object getSon(@PathVariable Integer id) {
		
		return queryOk(service.queryCategory(id, null, null, 0, 0));
	}
	
	@PutMapping("/")
	@ApiOperation("新增分类")
	public Object addCategory(@RequestBody CategoryVo category) {
		Category c = CategoryVo.convert(null, category);
		service.addCategory(c);
		
		return addOk(service.queryCategoryById(c.getId()));
	}
	
	@PutMapping("/{id}")
	@ApiOperation("修改分类")
	public Object addCategory(@PathVariable Integer id, @RequestBody CategoryVo category) {
		Category c = CategoryVo.convert(id, category);
		service.changeCategory(c);
		
		return addOk(service.queryCategoryById(c.getId()));
	}
	
	@DeleteMapping("/{id}")
	@ApiOperation(value = "删除分类", notes = "可以删除多个分类")
	public Object deleteCategory(@PathVariable Integer[] id) {
		service.deleteCategory(id);
		
		return deleteOk();
	}
	
	
	
}
